import Vuex from 'vuex'
const namespaced={namespaced: true}
const state = {
    lists: [{ data: 0, name: "hehe" }],
    loading:false,

}
//新vuex强制加了这个,和组件里面的mapGetters相对应,
//这个是必须的！！！！！！！！！！！，虽然方便，但是头大~~~~~~
const getters = {
    list(state) {
        return state.lists
    },
    loading(state){
        return state.loading
    }
}
const mutations = {
    push(state, data) {
        console.log("mutations接受到的数据", data)
        state.lists.push(data);
        console.log("after:", state.lists)
    },
    loadState(state,data){
        state.loading=data;
    }
}
const actions = {
    pushPromise({ dispatch, commit }, data) {
        console.log("ajax获取到的参数", data)
        //模拟异步请求
        return new Promise((resole, reject) => {
            setTimeout(() => {
                resole(data)
            }, 1000)
        });
    },
    push({ dispatch, commit }, data) {
        commit("loadState",true)
        //模拟异步请求
        return dispatch('pushPromise', data).then(Response => {
            console.log("ajax响应的数据", Response)
            commit('push', Response)
            commit("loadState",false)
        })
    }
}

export default {
    state,
    mutations,
    actions
    ,getters
};